﻿package cn.flashk.net
{
    import cn.flashk.events.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class LoaderManager extends EventDispatcher
    {
        private var uldrs:Array;
        private var index:uint = 0;
        private var datas:Array;
        private var isStart:Boolean = false;
        private var loadCout:uint = 0;
        private static var ins:LoaderManager;
        public static var max:uint = 10;

        public function LoaderManager()
        {
            var urlLoader:URLLoader = null;
            uldrs = new Array();
            var count:int = 0;
            while (count < max)
            {
                
				urlLoader = new URLLoader();
				urlLoader.addEventListener(Event.COMPLETE, completeDispath);
                uldrs[count] = urlLoader;
				count++;
            }
            datas = new Array();
            return;
        }// end function

        public function load(param1:String, param2:String = "global", type:String = "binary") : void
        {
            datas.splice(index + loadCout, 0, [param1, param2, type]);
            if (isStart == false)
            {
                startLoad(0);
                isStart = true;
            }
            var _loc_5:* = loadCout + 1;
            loadCout = _loc_5;
            return;
        }// end function

        public function add(param1:String, param2:String = "global", param3:String = "binary") : void
        {
            datas.push([param1, param2, param3]);
            if (index == (datas.length - 1))
            {
                startLoad(0);
            }
            return;
        }// end function

        private function startLoad(param1:uint) : void
        {
            if (index + param1 >= datas.length)
            {
                return;
            }
            var _loc_2:* = new URLRequest(datas[index + param1][0]);
            URLLoader(uldrs[param1]).dataFormat = datas[index + param1][2];
            URLLoader(uldrs[param1]).load(_loc_2);
            return;
        }// end function

        private function completeDispath(event:Event) : void
        {
            var _loc_2:* = getID(event.currentTarget as URLLoader);
            try
            {
                this.dispatchEvent(new LoaderManagerEvent(LoaderManagerEvent.COMPLETE, datas[index + _loc_2][0], datas[index + _loc_2][1], URLLoader(event.currentTarget)));
            }
            catch (e:Error)
            {
            }
            var _loc_4:* = index + 1;
            index = _loc_4;
            setTimeout(startLoad, 20, 0);
            return;
        }// end function

        private function getID(urlLoader:URLLoader) : uint
        {
            var _loc_2:uint = 0;
            var _loc_3:int = 0;
            while (_loc_3 < max)
            {
                
                if (uldrs[_loc_3] == urlLoader)
                {
                    _loc_2 = _loc_3;
                    break;
                }
                _loc_3++;
            }
            return _loc_2;
        }// end function

        public static function getInstance() : LoaderManager
        {
            if (ins == null)
            {
                ins = new LoaderManager;
            }
            return ins;
        }// end function

    }
}
